package cn.edu.cdtu.healthclient.connect;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import cn.edu.cdtu.healthclient.domain.HealthFileBody;
import cn.edu.cdtu.healthclient.domain.HealthFileMental;
import cn.edu.cdtu.healthclient.utils.ConnectUtil;
import cn.edu.cdtu.healthclient.utils.ConnectUtil.HealthFilePargrams;

import com.google.gson.Gson;

public class ConnectForHealthFile {
	
	public HealthFileMental getHealthFileMental(HttpClient client) {
		//创建一个Gson对象 （GSON是Google开发的Java API，用于转换Java对象和Json对象。）
		Gson gson = new Gson();
		//创建一个Http连接，利用ConnectUtil类中的值组合成URL，URL指向服务器上的健康文件类容。
		HttpPost httpPost = new HttpPost(
				ConnectUtil.HealthServerContect.SERVER_URL
				+ConnectUtil.HealthServerContect.HEALTH_FILE_SERVLET);
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		// 添加要传递的参数
		NameValuePair pair = new BasicNameValuePair(
				HealthFilePargrams.Pargram.HEALTH_FILE_TYPE_ID, "1");
		pairs.add(pair);
		try {
			HttpEntity httpEntity = new UrlEncodedFormEntity(pairs, "UTF-8");
			httpPost.setEntity(httpEntity);
			HttpResponse httpResponse = client.execute(httpPost);
			//如果连接成功返回一个包含所需数据的JSON对象，
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				String result = EntityUtils.toString(httpResponse.getEntity(),
						"UTF-8");
				return gson.fromJson(result, HealthFileMental.class);
			}

		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return null;
	}

	public HealthFileBody getHealthFileBody(HttpClient client) {
		Gson gson = new Gson();
		HttpPost httpPost = new HttpPost(
				ConnectUtil.HealthServerContect.SERVER_URL
				+ConnectUtil.HealthServerContect.HEALTH_FILE_SERVLET);
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		// 添加要传递的参数
		NameValuePair pair = new BasicNameValuePair(
				HealthFilePargrams.Pargram.HEALTH_FILE_TYPE_ID, "2");
		pairs.add(pair);
		try {
			HttpEntity httpEntity = new UrlEncodedFormEntity(pairs, "UTF-8");
			httpPost.setEntity(httpEntity);
			HttpResponse httpResponse = client.execute(httpPost);
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				String result = EntityUtils.toString(httpResponse.getEntity(),
						"UTF-8");
				return gson.fromJson(result, HealthFileBody.class);
			}

		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
